// pages/play/play.js
var api = require('../../utils/api.js')
var util = require('../../utils/util.js')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      file:{},
      item:{},
      isPlaying: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if(options.songid){
      util.playSong(options.songid).then((data,res) => {
        that.setData(Object.assign({isPlaying: true},data))
        wx.setStorage({
          key: 'playing',
          data: data,
          fail:function(){
            console.log('存储失败!!')
          }
        })
      })
    }else{
     wx.getStorage({
        key: 'playing',
        success: (res) => {
          that.setData(res.data)
        }
      })
    }

    wx.getStorage({
      key: 'isPlaying',
      success: function(res) {
        that.setData({isPlaying: res.data})
      },
    })
  },
  playToggle(e){
    let that = this
    if(that.data.isPlaying){
      wx.stopBackgroundAudio()
      that.setData({isPlaying: false})
    }else{
      util.playSong(that.data.item.song_id).then(data => {
        that.setData({
          isPlaying: true
        })
      })
    }
  }
})